package org.jaaksi.demo.animator;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

/**
 * Created by Fcy on 2015/10/1.
 */
public class KugouAnimation extends Activity {

    private ImageView ball1;
    private ImageView ball2;
    private ImageView ball3;
    private ImageView ball4;
    private final int BALL_WIDTH = 30;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animator_kugou);

        ball1 = (ImageView) findViewById(R.id.ball1);
        ball2 = (ImageView) findViewById(R.id.ball1);
        ball3 = (ImageView) findViewById(R.id.ball1);
        ball4 = (ImageView) findViewById(R.id.ball1);
    }

    public void clickBall(View view) {
        switch (view.getId()) {
            case R.id.ball1:
                startAnimator(0, view);
                break;
            case R.id.ball2:
                startAnimator(1, view);
                break;
            case R.id.ball3:
                startAnimator(2, view);
                break;
            case R.id.ball4:
                startAnimator(3, view);
                break;
        }
    }

    private void startAnimator(int pos, View ball) {
        ObjectAnimator translationY = ObjectAnimator
            .ofFloat(ball, "TranslationY", DensityUtil.dip2px(this, 300 + (4 - pos) * BALL_WIDTH))
            .setDuration(2000);
        translationY.setInterpolator(new AccelerateInterpolator());
        translationY.start();
        ObjectAnimator.ofFloat(ball, "TranslationX",
            DensityUtil.getWindowsWidth(this) / 2 - DensityUtil.dip2px(this, BALL_WIDTH / 2))
            .setDuration(2000)
            .start();

    }

    /**
     * 地面距离第四个球为300dp，没隔一个增加一个球高度30dp
     *
     * @param pos int 第几个邱
     */
    private void startAnimator2(int pos, View ball) {
        PropertyValuesHolder translationY = PropertyValuesHolder
            .ofFloat("TranslationY", DensityUtil.dip2px(this, 300 + (4 - pos) * BALL_WIDTH));
        PropertyValuesHolder translationX = PropertyValuesHolder
            .ofFloat("TranslationX", DensityUtil.dip2px(this, 200));
        ObjectAnimator.ofPropertyValuesHolder(ball, translationY, translationX)
            .setDuration(2000).start();

    }
}